<?

require_once("layout_config.php");
require_once("include/permission.php");
require_once("include/html_utils.php");

$user_id = permission_redirect_user("login.php");

if ($_SERVER['REQUEST_METHOD'] == "POST") {
	$ret = user_add($_REQUEST["name"], 
			$_REQUEST["lastname"], 
			$_REQUEST["email"], 
			$_REQUEST["alias"], 
			$_REQUEST["password"], 
			$_REQUEST["access_level"]);
	if (!$ret) {
		header("Location: users.php");
	} else {
		$error = "<font color='red'><b>"._("Unable to add user")."</b></font>";
	}
}

?>
	<script type='text/javascript'>
	$(document).ready(function(){
		$("#save").click(function() {
			$("#userForm").submit();
		});
		$("#userForm").validate({
			rules: {
				name: {
					required: true,
					minlength: 3
				},
				lastname: {
					required: true,
					minlength: 3
				},
				alias: {
					required: true,
					minlength: 5,
					remote: {
						url: "services/check_alias.php",
						type: "get",
						data: {
							alias: function() {
								return $("#alias").val();
							}
						}
					}
				},
				email: {
					email: true,
					required: true,
					remote: {
						url: "services/check_email_duplicates.php",
						type: "get",
						data: {
							email: function() {
								return $("#email").val();
							}
						}
					}
				},
				password: {
					required: true,
					minlength: 5
				},
				repeat_password: {
					required: true,
					minlength: 5,
					equalTo: "#password"
				}
			},
			messages: {
				name: {
					required: "<?=_("Please insert the user name.")?>",
					minlength: "<?=_("The name must be at least 3 characters long.")?>"
				},
				lastname: {
					required: "<?=_("Please insert the user lastname.")?>",
					minlength: "<?=_("The lastname must be at least 3 characters long.")?>"
				},
				email: {
					required: "<?=_("You must insert an email address")?>",
					email: "<?=_("Please enter a valid email")?>",
					remote: "<?=_("The email address is being used by another user.")?>"
				},
				password: {
					required: "<?=("A password is required.")?>",
					minlength: "<?=("The password must be at least 5 characters long.")?>"
				},
				repeat_password: {
					required: "<?=_("A password is required.")?>",
					minlength: "<?=_("The password must be at least 5 characters long.")?>",
					equalTo: "<?=_("Please enter the same password as above.")?>"
				},
				alias: {
					required: "<?=_("You must insert an alias.")?>",
					remote: "<?=_("The alias is already taken, please choose another.")?>",
					minlength: "<?=_("The alias must be at least 5 characters long.")?>"
				}
			}
		});
	});
</script>
<table width="100%" border="0" cellpadding="0" cellspacing="0" align="center">
	<tr>
		<td rowspan="3" class="form_left" width="40">&nbsp;</td>
		<td height="20" class="form_title">&nbsp;New user</td>
	</tr>
<?php
	if (isset($error)) {
		echo "<tr><td colspan='2'>";
		echo $error;
		echo "</td></tr>";
	}
?>
	<tr>
		<td>
		<br>
		<form id="userForm" method="post" action="user_add.php">
		<table border="0" cellpadding="1" cellspacing="1" align="center">
		<tr>
			<td align="right" height="20"><?=_("Name");?>:&nbsp;</td>
			<td width="176"><input type="text" name="name" id="name" class="form_input" /></td>
		</tr>
		<tr>
			<td align="right"><?=_("Lastname");?>:&nbsp;</td>
			<td><input type="text" name="lastname" id="lastname" class="form_input" /></td>
		</tr>
		<tr>
			<td align="right"><?=_("Alias");?>:&nbsp;</td>
			<td>
				<input type="text" name="alias" id="alias" class="form_input" />
			</td>
		</tr>
		<tr>
			<td align="right"><?=_("Password");?>:&nbsp;</td>
			<td><input type="password" name="password" id="password" class="form_input" /></td>
		</tr>
		<tr>
			<td align="right"><?=_("Repeat Password");?>:&nbsp;</td>
			<td>
				<input type="password" name="repeat_password" id="repeat_password" class="form_input" />
			</td>
		</tr>
		<tr>
			<td align="right"><?=_("Email");?>:&nbsp;</td>
			<td><input type="text" name="email" id="email" class="form_input" /></td>
		</tr>
		<tr>
			<td align="right"><?=_("Acess level");?>:&nbsp;</td>
			<td>
				<select name="access_level" id="access_level" class="form_input">
				<? echo html_select_accesslevellist(); ?>
				</select>
			</td>
		</tr>
		</table>
		</form>
		<table width="99%" border="0" cellpadding="1" cellspacing="1">
			<tr>
				<td colspan="2" class="form_separator">&nbsp;</td>
			</tr>
			<tr>
				<td align="right" height="40">
					<a href="#" id="save">
					<img src="themes/<?=$layout_theme; ?>/images/btn_save.jpg" border="0" />
					</a>
				</td>
				<td width="65">
					<a onClick="javascript:self.parent.tb_remove()" href="#">
					<img src="themes/<?=$layout_theme; ?>/images/btn_cancel.jpg" border="0" />
					</a>
				</td>
			</tr>
		</table>
		</td>
	</tr>
	<tr>
		<td class="form_left" height="4"> </td>
	</tr>
</table>
</body>
</html>
